//
//  GGTTeamBuyListViewController.m
//  GGT
//
//  Created by 空空空 on 14/11/10.
//  Copyright (c) 2014年 GGT. All rights reserved.
//

#import "GGTTeamBuyListViewController.h"
#import "DPRequest.h"
#import "GGTAppDelegate.h"
#import "GGTTeamBuyModel.h"
#import "GGTTeamBuyTableViewCell.h"
#import "GGTTeamBuyDetailViewController.h"
@interface GGTTeamBuyListViewController ()<UITableViewDataSource, UITableViewDelegate, DPRequestDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *array;
@end

@implementation GGTTeamBuyListViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.array = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.tableView.rowHeight = 120;
    self.tableView.delegate = self;
    self.tableView.dataSource =self;
    [self.view addSubview:self.tableView];
    NSString *url = @"v1/deal/find_deals";
    NSString *params = [NSString stringWithFormat:@"keyword=%@&city=%@", self.str, [[NSUserDefaults standardUserDefaults] objectForKey:@"cityName"]];
    [[[GGTAppDelegate instance] dpapi] requestWithURL:url paramsString:params delegate:self];

    
}
- (void)request:(DPRequest *)request didFinishLoadingWithResult:(id)result
{
    [self.array removeAllObjects];
    
    NSMutableArray *arr = [result objectForKey:@"deals"];
    for (NSDictionary *dic in arr) {
        GGTTeamBuyModel *teamBuy = [[GGTTeamBuyModel alloc]initWithDictionary:dic];
        [self.array addObject:teamBuy];
    }
    [self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GGTTeamBuyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ll"];
    if (cell == nil) {
        cell = [[[GGTTeamBuyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ll"]autorelease];
    }
    GGTTeamBuyModel *model = [self.array objectAtIndex:indexPath.row];
    cell.GGTTeamBuy = model;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GGTTeamBuyDetailViewController *detail = [[GGTTeamBuyDetailViewController alloc]init];
    [self.navigationController pushViewController:detail animated:YES];
    detail.GGTTeamBuyDetail = [self.array objectAtIndex:indexPath.row];
    [detail release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
